#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QScreen>
#include <QDebug>
#include <QToolTip>
#include <QPixmap>
#include <QList>
#include <vector>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(triggered()),this,SLOT(on_pushButton_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}
QImage Binaryzation(const QImage &origin)
{
    int width = origin.width();
    int height = origin.height();
    QImage newImg = QImage(width, height, QImage::Format_RGB888);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int gray = qGray(origin.pixel(x, y));
            int newGray;
            if (gray > 128)
                newGray = 255;
            else
                newGray = 0;
            newImg.setPixel(x, y, qRgb(newGray, newGray, newGray));
        }
    }
    return newImg;
}

void MainWindow::on_pushButton_clicked()
{
    QString OpenFile, OpenFilePath;
        QImage image;
        //打开文件夹中的图片文件
        OpenFile = QFileDialog::getOpenFileName(this,
                                                  "Please choose an image file",
                                                  "",
                                                  "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
        if( OpenFile != "" )
        {
            if( image.load(OpenFile) )
            {
                ui->image->setScaledContents(true);
                ui->image->setPixmap(QPixmap::fromImage(Binaryzation(image)));
            }
        }

}


void MainWindow::on_pushButton_2_clicked()
{
    QString OpenFile, OpenFilePath;
        QImage image;
        //打开文件夹中的图片文件
        OpenFile = QFileDialog::getOpenFileName(this,
                                                  "Please choose an image file",
                                                  "",
                                                  "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
        if( OpenFile != "" )
        {
            if( image.load(OpenFile) )
            {
                ui->image->setScaledContents(true);
                ui->image->setPixmap(QPixmap::fromImage(image));
            }
        }
}

